package com.example.demo;

import com.example.demo.service.MyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;

@SpringBootApplication(exclude = {WebMvcAutoConfiguration.class}) // 禁用Web服务器
public class DemoApplication implements CommandLineRunner {
    @Autowired
    private MyService myService;

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        // 处理命令行参数
        if (args.length == 0) {
            System.out.println("请提供命令行参数！");
            System.out.println("用法: java -jar app.jar [command] [arguments]");
            return;
        }

        String command = args[0];

        switch (command) {
            case "greet":
                if (args.length > 1) {
                    String name = args[1];
                    String result = myService.greet(name);
                    System.out.println(result);
                } else {
                    System.out.println("greet命令需要提供一个名称参数");
                }
                break;
            case "calculate":
                if (args.length > 2) {
                    int a = Integer.parseInt(args[1]);
                    int b = Integer.parseInt(args[2]);
                    int sum = myService.calculate(a, b);
                    System.out.println("计算结果: " + sum);
                } else {
                    System.out.println("calculate命令需要提供两个数字参数");
                }
                break;
            default:
                System.out.println("未知命令: " + command);
                System.out.println("支持的命令: greet, calculate");
        }
    }

}
